package com.mhxy.util;

import javafx.event.ActionEvent;
import javafx.scene.Node;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;

import java.util.Optional;

public class AppUtil {

    public static void alter(ActionEvent event, String title, String headerText, String content) {
        // 获取当前窗口的 Stage
        Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        // 设置 Alert 的 owner 为当前窗口
        alert.initOwner(stage);

        alert.setTitle(title);
        alert.setHeaderText(headerText);
        alert.setContentText(content);
        alert.showAndWait();
    }

    public static void alter(MouseEvent event, String title, String headerText, String content) {
        // 获取当前窗口的 Stage
        Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        // 设置 Alert 的 owner 为当前窗口
        alert.initOwner(stage);

        alert.setTitle(title);
        alert.setHeaderText(headerText);
        alert.setContentText(content);
        alert.showAndWait();
    }

    public static boolean confirm(ActionEvent event, String title, String headerText, String content) {
        // 获取当前窗口的 Stage
        Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
        Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
        // 设置 Alert 的 owner 为当前窗口
        alert.initOwner(stage);

        alert.setTitle(title);
        alert.setHeaderText(headerText);
        alert.setContentText(content);
        Optional<ButtonType> result = alert.showAndWait();
        return result.isPresent() && result.get() == ButtonType.OK;
    }

    public static boolean confirm(MouseEvent event, String title, String headerText, String content) {
        // 获取当前窗口的 Stage
        Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
        Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
        // 设置 Alert 的 owner 为当前窗口
        alert.initOwner(stage);

        alert.setTitle(title);
        alert.setHeaderText(headerText);
        alert.setContentText(content);
        Optional<ButtonType> result = alert.showAndWait();
        return result.isPresent() && result.get() == ButtonType.OK;
    }
}
